package org.winexpress

import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject
import org.codehaus.groovy.grails.web.json.JSONArray

class CartItemController {

    // shared methods
    def sendResponse(statusCode) {
        if (statusCode == 404) {
            JSONObject o = new JSONObject()
            o.put(statusCode, true)
            render o as JSON
        }
        
        response.status = statusCode
        render response
        
        // 200-OK (e.g. GET success)
        // 201-Created (e.g. POST success)
        // 403-Forbidden (e.g. LOGIN failure)
        // 400-Bad Request (e.g. PUT,POST,DELETE failure)
        // 404-Not Found (e.g. GET failure)
        // 500-Internal Server Error (other...)
    }
    

    // REST operations
    // GET
    def show = {
        println 'CartItemController >> Item Show >> START'
        
        CartItemManagement ciman = new CartItemManagement()
        
        if (params.itemId && CartItem.exists(params.itemId)) {    
            def ci = ciman.getCartItemByID(params.itemId)
            render ci as JSON
            sendResponse(200)
            
            println 'Show shopping cart Item by ID >> ' + ci.id + " " + ci
        }
        else {
            def allCI = ciman.listCartItems()
            if (allCI.length() > 0) {
                render allCI as JSON
                sendResponse(200)
            }
            else {
                sendResponse(404)
            }  
            
            println 'Listing all shopping cart Item >> ' + allCI
        }
        
        println 'CartItemController >> Item Show >> END'
    }
    
    
    
    
    // PUT
    def update = {
        println 'CartItemController >> Item Update >> START'
        
        CartItemManagement ciman = new CartItemManagement()
           
        JSONObject o = request.JSON
        
        def customerId = o.get("customerId")
        def productId = o.get("productId")
        def cartId = o.get("cartId")
        
        println "cartitem controller: " + o
        
        Customer user = Customer.findById(customerId)
        def carts = user.shoppingCarts.toList()
        def cart = carts[0]
            
        def ci = ciman.getCartItemByID(o.get("id"))
        if (o.has("quantity")) { ci.quantity = o.get("quantity").toInteger() }
        
        def result = ciman.updateCartItem(i)
        if (result == true) {
            println 'CartItemController >> Item Update >> ' + result
            sendResponse(200)
        }
        else {
            println 'CartItemController >> Item Update >> ' + result
            sendResponse(400)
        }
        

        println 'CartItemController >> Item Update >> END'

    }
    
    
    def updateQuantity = {
        println 'CartItemController >> Item Update >> START'
        
        CartItemManagement ciman = new CartItemManagement()
        
        def cartItemId = params['cartItemId']
        def productId = params['productId']
        def quantity = params['quantity']
        println 'cartItemId:: ' + cartItemId
        println 'productId:: ' + productId
        println 'quantity:: ' + quantity
        
        def result = ciman.updateQuantity(cartItemId, quantity)
        if (result == true) {
            println 'CartItemController >> Item Update >> ' + result
            sendResponse(200)
        }
        else {
            println 'CartItemController >> Item Update >> ' + result
            sendResponse(400)
        }
        

        println 'CartItemController >> Item Update >> END'

    }
    
    
    // DELETE
    def delete = {
        println 'CartItemController >> Item delete >> START'
        
        CartItemManagement ciman = new CartItemManagement()
        
        if (params.itemId && CartItem.exists(params.itemId)) {    
            def result = ciman.deleteCartItem(itemId)
            
            if (result == true) {
                println 'CartItemController >> Item delete >> ' + result
                sendResponse(200)
            }
            else {
                println 'CartItemController >> Item delete >> ' + result
                sendResponse(400)
            }
        }
        
        println 'CartItemController >> Item delete >> END'
    }
    
    
    // POST
    def save = {
        println 'CartItemController >> Item Save >> START'
        
        JSONObject o = request.JSON
        
        def customerId = o.get("customerId")
        def productId = o.get("productId")
        def cartId = o.get("cartId")
        def quantity = o.get("quantity")
        
        
        CartItemManagement ciman = new CartItemManagement()
        Customer user = Customer.findById(customerId)
        def carts = user.shoppingCarts.toList()
        def cart = carts[0]
        
        def result = false
        
        if (cart.cartItems.size() == 0) {
            result = ciman.addCartItem(o)
        }
        else {
            cart.cartItems.each()
            {
                println 'compare exist and added :: ' + it.product.id + ' and ' + productId.toInteger()

                if (it.product.id == productId.toInteger()) {
                    println 'go to update'
                    forward(controller:"cartItem", action:"updateQuantity", params:[cartItemId: it.id, productId:productId, quantity:quantity])
                }
                else {
                    result = ciman.addCartItem(o)
                }
            }
        }
        
        if (result == true) {
            println 'CartItemController >> Item Save >> ' + result
            sendResponse(201)
        }
        else {
            println 'CartItemController >> Item Save >> ' + result
            sendResponse(400)
        }
        
        println 'CartItemController >> Item Save >> END'
    }
    
}
